package com.example.myui.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myui.R;

public class HandlerActivity extends AppCompatActivity {
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case 1001:
                    Log.i(TAG, "handleMessage: " + Thread.currentThread());
                    countTime--;
                    if (countTime > 0) {
                        time.setText(countTime + "s");
                        Message message = new Message();
                        message.what = 1001;
                        this.sendMessageDelayed(message, 1000);
                    } else {
                        time.setText("over");
                    }
                    break;
                case 1002:
                    if (index % 3 == 0) {
                        image.setImageResource(images[0]);
                    } else if (index % 3 == 1) {
                        image.setImageResource(images[1]);
                    } else {
                        image.setImageResource(images[2]);
                    }
                    index++;
                    Message message = new Message();
                    message.what = 1002;
                    this.sendMessageDelayed(message, 1000);
                    if (index == 20) {
                        this.removeMessages(1001);
                    }
                    break;

                case 1003:
                    Log.i(TAG, "handleMessage: " + "UI---->" + Thread.currentThread());
                    threadHandler.sendEmptyMessageDelayed(1, 1000);
                    break;
                case 1004:
                    String name = (String) msg.obj;
                    result.append("当前线程---------->" + name + "\n");
                    myHandler.sendEmptyMessageDelayed(1, 1000);
                    break;
                case 1005:
                    String name1 = (String) msg.obj;
                    result.append("当前线程---------->" + name1 + "\n");
                    break;

                default:
                    Log.i(TAG, "handleMessage: " + Thread.currentThread().getName());
                    myHandler.sendEmptyMessage(1);
                    break;
            }

        }
    };

    private TextView time;
    private int countTime = 60;
    private int[] images = {R.mipmap.bg_girl, R.mipmap.bg_monkey, R.mipmap.bg_monkey_king};
    private int index = 0;
    private ImageView image;
    private String TAG = HandlerActivity.class.getSimpleName();
    private HandlerThread handlerthread;
    private Handler threadHandler;
    private TextView result;
    private Handler myHandler;
    private HandlerThread handlerThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);
        time = findViewById(R.id.tv_time);
        image = findViewById(R.id.image);
        handlerthread = new HandlerThread("handlerthread");
        handlerthread.start();
        threadHandler = new Handler(handlerthread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                Log.i(TAG, "handleMessage: " + Thread.currentThread());
                Message message = new Message();
                message.what = 1003;
                mHandler.sendMessageDelayed(message, 1000);
            }
        };

//        与线程先关的handler
        handlerThread = new HandlerThread("my---handler thread");
        handlerThread.start();
        myHandler = new Handler(handlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                Log.i(TAG, "handleMessage: " + Thread.currentThread().getName());
                mHandler.sendEmptyMessage(1);
            }
        };

        findViewById(R.id.btn_main_handler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.sendEmptyMessage(1);
            }
        });


        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = 1003;
                mHandler.sendMessageDelayed(message, 1000);
//                threadHandler = new Handler(handlerthread.getLooper()) {
//                    @Override
//                    public void handleMessage(Message msg) {
//                        Message message = new Message();
//                        message.what = 1003;
//                        mHandler.sendMessageDelayed(message, 1000);
//                    }
//                };

            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyThread myThread = new MyThread();
                myThread.start();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                myThread.threadHandler.sendEmptyMessage(1);
                Message message = new Message();
                message.what = 1001;
                mHandler.sendMessageDelayed(message, 1);
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time.setText(countTime + "s");
                Message message = new Message();
                message.what = 1001;
                mHandler.sendMessageDelayed(message, 1000);
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = 1002;
                mHandler.sendMessageDelayed(message, 1000);

            }
        });


        findViewById(R.id.btn_thread_handler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        result = findViewById(R.id.tv_result);
    }


    //线程相关handler
    class MyThread extends Thread {
        private Handler threadHandler;

        @SuppressLint("HandlerLeak")
        @Override
        public void run() {
            Looper.prepare();
            threadHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Log.i(TAG, "handleMessage: " + Thread.currentThread());
                }
            };
            Looper.loop();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
